package cn.edu.zhku.citrusmonitor.qa.controller;


import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.domain.TbAnswer;
import cn.edu.zhku.citrusmonitor.domain.TbExpert;
import cn.edu.zhku.citrusmonitor.qa.service.AnswerService;
import cn.edu.zhku.citrusmonitor.qa.service.AnswerServiceImpl;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.PageBean;
import cn.edu.zhku.citrusmonitor.util.UUIDUtils;

import net.sf.json.JSONObject;

@WebServlet("/answer.do")
public class AnswerServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	AnswerService as = new AnswerServiceImpl();
	
	
	public JSONObject addA(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException, SQLException{
		

			
			TbAnswer ta= new TbAnswer();
			//需要answer_id,question_id,expert_id,responser,content
			BeanUtils.populate(ta, request.getParameterMap());//这里要有	question_id，content
			TbExpert expert=(TbExpert) request.getSession().getAttribute("expert");
			if(expert!=null) {
			Integer expertId=expert.getExpertId();
			String responder=expert.getExpertName();			
			ta.setExpertId(expertId);
			ta.setResponder(responder);
			}
			//设置唯一的answerid
			ta.setAnswerId(UUIDUtils.getId());			
			as.addA(ta);			
			
			return JSONObject.fromObject(new Message("1", "问题回答成功"));
			
		
		
		
		
	
	}
	
	public JSONObject modifyA(HttpServletRequest request, HttpServletResponse response) throws SQLException, IllegalAccessException, InvocationTargetException{
		
			TbAnswer ta= new TbAnswer();
			//需要answer_id,content
			BeanUtils.populate(ta, request.getParameterMap());//这里要有	answer_id，content
			
			if(as.modifyA(ta)==1) {
				return JSONObject.fromObject(new Message("1", "修改回答成功"));
			}
			return JSONObject.fromObject(new Message("2", "修改回答失败"));	
			

		
		
		
	}
	
	//批量删除
			public JSONObject delAll(HttpServletRequest request, HttpServletResponse response) throws SQLException{
				String[] ids = request.getParameterValues("ids");
				
					if(as.delAll(ids)==1)
					{
						return JSONObject.fromObject(new Message("1", "删除公告成功"));
					}
					return JSONObject.fromObject(new Message("2", "删除公告失败"));
				
			}
	
	
	public JSONObject removeA(HttpServletRequest request, HttpServletResponse response) {
		String answerId=request.getParameter("answerId");
		
		try {
			if(as.removeA(answerId)==1) {
				
				return JSONObject.fromObject(new Message("1", "修改回答成功"));
			}
			return JSONObject.fromObject(new Message("2", "修改回答失败"));	
		} catch (Exception e) {
			return JSONObject.fromObject(new Message("2", "修改回答失败"));	
		}

	
	}
	//Ajax搜索框显示未回复的问题
	public String findAByWord(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
		
			String word=request.getParameter("word");
			List<TbAnswer> list = new ArrayList<TbAnswer>();
			list=as.findAByWord(word);
			if(list.size()!=0) {
				return JSONArray.toJSONString(list);	
				
			}
			return JSON.toJSONString(new Message("2", "无相关回答"));
		
	
	}
	//根据questionId搜索相应的答案
	
	public String showA(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
			
			
			String questionId=request.getParameter("questionId");
			
			TbAnswer answer = as.showA(questionId);
		
			return JSON.toJSONStringWithDateFormat(answer, "yyyy-MM-dd", SerializerFeature.EMPTY);			
					
	}
	
	
	
	
	//判断专家是否已经登录
	public int loadStatus(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		TbExpert expert= (TbExpert) request.getSession().getAttribute("expert");
		
	
		if(expert==null) {
			return 0;
		}
		else {
			int expertId=expert.getExpertId();
			String expertPassword =as.findExpertPasswordById(expertId);
			if( expertPassword.equals(expert.getPassword())) {
				return 1;
			}
			else
				return 0;

		}
		
	}
	
	//后台管理，显示全部
			public String showAll(HttpServletRequest request, HttpServletResponse response) throws SQLException {
				
				int currentPage=0;
				String currentPage1=request.getParameter("currentPage");
				if(currentPage1!=null) {
					currentPage = Integer.parseInt(currentPage1);
				}
				else {
					currentPage=4;
				}
				
				int pageSize=0;
				String pageSize1=request.getParameter("pageSize");
				if(pageSize1!=null) {
					pageSize = Integer.parseInt(pageSize1);
				}
				else {
					pageSize=4;
				}
				
				PageBean<TbAnswer> pb=new PageBean<TbAnswer>();
				pb=as.showAll(currentPage,pageSize);			
				return JSON.toJSONString(pb);
				
			}

	
}
